package com.thinkgem.jeesite.modules.export.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.export.entity.ExportChildhelp;
import com.thinkgem.jeesite.modules.export.service.ExpRoutinevacc6_1Service;
import com.thinkgem.jeesite.modules.export.service.Export6_2Service;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SysAreaService;
import com.thinkgem.jeesite.modules.sys.utils.TimeUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value = "${adminPath}/export/export")
public class ExportController extends BaseController {
	
	@Autowired
	SysAreaService sysAreaService;
	@Autowired
	OfficeService officeService;
	@Autowired
	ExpRoutinevacc6_1Service expRoutinevacc6_1Service;
	@Autowired
	Export6_2Service export6_2Service;
	
	/**
	 *进入 routineVacc6_1 的弹出框
	 * @author xuejinshan
	 * @date 2017年9月7日 下午5:19:58
	 * @description 
	 * @return
	 *
	 */
	
	@RequestMapping("openbox6_1")
	public String openboxfor6_1(ExportChildhelp exportChildhelp,Model model,String yearstr,
			String monthstr ,String liveProp,String showType) {
		return "modules/export/openbox6_1";
	}
	
	/**
	 * 常规免疫6-1表数据处理 Controller
	 * @author Jack
	 * @date 2017年10月12日 下午1:49:01
	 * @description 
	 * @param exportChildhelp
	 * @param model
	 * @param yearstr
	 * @param startmonthstr
	 * @param endmonthstr
	 * @param live
	 * @param showType
	 * @param typename
	 * @param vaccCode
	 * @param vaccname
	 * @return
	 *
	 */
	@RequestMapping("routineVacc6_1")
	public String routineVacc6_1(ExportChildhelp exportChildhelp,Model model, String live) {
		//初始化页面基本信息
		expRoutinevacc6_1Service.setInfo(exportChildhelp, model , live);
		model.addAttribute("xAxisData", "0");
		model.addAttribute("seriesStr", "0");
		return "modules/export/routineVacc6_1";
	}
	
	/**
	 * 返回6-1数据
	 * @author Jack
	 * @date 2018年3月26日 下午4:56:10
	 * @description 
	 * @param exportChildhelp
	 * @param model
	 * @param live
	 * @return
	 *
	 */
	@RequestMapping("getRoutineVacc6_1Data")
	public String getRoutineVacc6_1Data(ExportChildhelp exportChildhelp,Model model, String live) {
		String officeId = exportChildhelp.getOfficeId();
		HashMap<String, Object> returnMap = expRoutinevacc6_1Service.getNextLevelList(officeId);
		@SuppressWarnings("unchecked")
		List<Office> nextLevelList = (List<Office>)returnMap.get("nextLevelList");
		if(nextLevelList != null){
			//当前节点不是根子节点
			expRoutinevacc6_1Service.setInfo(exportChildhelp, model , live);
			model.addAttribute("maps", JsonMapper.toJsonString(""));
			HashMap<String, Object> resultMap = expRoutinevacc6_1Service.getNotEndChildNodeData(officeId, exportChildhelp.getStartTime(), exportChildhelp.getEndTime()); //获取选中非根子节点时界面要展示的数据
			
			String resultStr = (String) resultMap.get("resultString");
			model.addAttribute("next_maps",JsonMapper.toJsonString(resultStr));
			HashMap<String, Object> echartsDataMap = expRoutinevacc6_1Service.getEchartsData(resultMap);
			
			model.addAttribute("minRE", echartsDataMap.get("minRE"));
			model.addAttribute("minSH", echartsDataMap.get("minSH"));
			model.addAttribute("minSR", echartsDataMap.get("minSR"));
			model.addAttribute("maxRE", echartsDataMap.get("maxRE"));
			model.addAttribute("maxSH", echartsDataMap.get("maxSH"));
			model.addAttribute("maxSR", echartsDataMap.get("maxSR"));
			model.addAttribute("xAxisData", echartsDataMap.get("xAxisData"));
			model.addAttribute("seriesStr", echartsDataMap.get("seriesStr"));
			return "modules/export/routineVacc6_1";
		}else{
			//当前节点是根子节点
			model.addAttribute("xAxisData", "['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0']");
			model.addAttribute("seriesStr", "['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0']");
			expRoutinevacc6_1Service.setInfo(exportChildhelp, model , live);
			HashMap< String , String> map = new HashMap<String, String>();
			map.put("startTime",exportChildhelp.getStartTime());
			map.put("endTime",exportChildhelp.getEndTime());
			
			if(StringUtils.isNotBlank(exportChildhelp.getVaccCode())){
				if(StringUtils.isNotBlank(exportChildhelp.getShowType())&& StringUtils.isNotBlank(live)){
					
					if(exportChildhelp.getShowType().equals("3")){
						//按居住属性
						List<HashMap<String, String>> live_maps = expRoutinevacc6_1Service.selectVaccData61_unit(map);
						model.addAttribute("live_maps",JsonMapper.toJsonString(live_maps));
					}else if(exportChildhelp.getShowType().equals("0")&& live.equals("1")){
						//按居委名称-按居住属性
						List<HashMap<String, String>> shequ_live_maps = expRoutinevacc6_1Service.selectVaccData61_unitAndReside(map);
						model.addAttribute("shequ_live_maps",JsonMapper.toJsonString(shequ_live_maps));
					}else if(exportChildhelp.getShowType().equals("0")&&live.equals("0")){
						//按居委名称-不按居住属性
						if(exportChildhelp.getTypeName().equals("MCV") || exportChildhelp.getTypeName().equals("RCV") || exportChildhelp.getTypeName().equals("MumCV") || exportChildhelp.getTypeName().equals("HepA")){
							List<HashMap<String, String>> maps = expRoutinevacc6_1Service.selectVaccSpecData61_unitNoReside(map); //组合类疫苗(MCV,RCV,MumCV,HepA)
							model.addAttribute("maps",JsonMapper.toJsonString(maps));
						}else{
							List<HashMap<String, String>> maps = expRoutinevacc6_1Service.selectVaccData61_unitNoReside(map); //常规类型
							model.addAttribute("maps",JsonMapper.toJsonString(maps));
						}
					}else{
						//按常规修订-不按居住属性   //按常规修订-按居住属性  //按特殊修订-不按居住属性  //按特殊修订-按居住属性
						List<HashMap<String, String>> all_maps = expRoutinevacc6_1Service.selectVaccData61_all(map);
						model.addAttribute("all_maps",JsonMapper.toJsonString(all_maps));
					}
				}else{
					return "modules/export/routineVacc6_1";
				}
			}else{
				return "modules/export/routineVacc6_1";
			}
			model.addAttribute("liveProp",live);
			model.addAttribute("date", DateUtils.formatDate(new Date(), "yyyy年MM月dd日"));
			return "modules/export/routineVacc6_1";
		}
		
	}
	
	/**
	 * 常规免疫接种情况6-1汇总表 数据初始化
	 * @author Jack
	 * @date 2017年10月24日 下午3:08:53
	 * @description 
	 *
	 */
	@RequestMapping("routineVacc6_1/initData")
	public @ResponseBody  String initRoutine6_1Data(HttpServletRequest request){
		//distinct查询SYS_COMMUNITY表中的所有LOCALCODE,遍历执行所有站点数据生成
		List<HashMap<String, String>> communityList = expRoutinevacc6_1Service.selectCommunityListNetVersion();
		
		if(request.getParameter("time") != null){
			String time = request.getParameter("time");
			int year = Integer.valueOf(time.substring(0, 4));
			int month = Integer.valueOf(time.substring(4, time.length()));
			for(HashMap<String, String> map : communityList){
				String localCode = map.get("LOCALCODE");
				expRoutinevacc6_1Service.getDataByMonth(year + "", month + "", localCode);
			}
			return "Hello,常规免疫接种情况6-1汇总表 " + year +"年" + month + "月份数据初始化完成!";
		}else{
			return "请在请求中带入时间参数,参数形式为: XXX/routineVacc6_1/initData?time=XXXXXX(或XXXXX),\n说明:前4位为年,后面一位或两位为月";
		}
	}
	
	/**
	 * 进入 routineVacc6_2 的弹出框
	 * @author xuejinshan
	 * @date 2017年9月8日 下午5:39:02
	 * @description 
	 * @param exportChildhelp
	 * @param model
	 * @param yearstr
	 * @param monthstr
	 * @param liveProp
	 * @param showType
	 * @return
	 *
	 */
	@RequestMapping("openbox6_2")
	public String openboxfor6_2(ExportChildhelp exportChildhelp,Model model,String yearstr,
			String monthstr ,String liveProp,String showType) {
		return "modules/export/openbox6_2";
	}
	
	
	/**
	 * 常规免疫接种情况6-2汇总表
	 * @author Jack
	 * @date 2017年10月20日 下午4:28:01
	 * @description
	 * @param exportChildhelp
	 * @param model
	 * @param yearstr
	 * @param startmonthstr
	 * @param endmonthstr
	 * @param vaccCode
	 * @param vaccname
	 * @return 
	 *
	 */
	@RequestMapping("routineVacc6_2")
	public String routineVacc6_2(ExportChildhelp exportChildhelp,Model model) {
		return export6_2Service.collectDataService(exportChildhelp, model);
	}
		
	/**
     * 3-1系列公用方法<br>绑定时间,地区,居住属性,显示类型等信息
     * @author xuejinshan
     * @date 2017年8月30日 下午10:26:58
     * @description 
     *
     */
    public void controllerHelp(Model model,ExportChildhelp exportChildhelp ,String yearstr,String monthstr,String liveProp,String showType){
    	//处理 省、市、区信息
    	String code = officeService.get(OfficeService.getFirstOffice()).getCode();//3406030301
    	if(StringUtils.isNotBlank(code)){
			String sheng = StringUtils.substring(code, 0, 2);
			String shi = StringUtils.substring(code, 2, 4);
			String qu = StringUtils.substring(code, 4, 6);
			model.addAttribute("sheng", sysAreaService.getAreaNameById(Integer.parseInt(sheng+"0000")).getName());
			model.addAttribute("shi", sysAreaService.getAreaNameById(Integer.parseInt(sheng+shi+"00")).getName());
			model.addAttribute("qu", sysAreaService.getAreaNameById(Integer.parseInt(sheng+shi+qu)).getName());
		}
		//处理和绑定时间
		if(StringUtils.isNotBlank(yearstr) && StringUtils.isNotBlank(monthstr)){
			if(StringUtils.isNotBlank(liveProp)){
				if(liveProp.equals("0")){
					exportChildhelp.setReside(null);
				}else{
					exportChildhelp.setReside(liveProp);
				}	
			}else{
				exportChildhelp.setReside("1");
			}
			if(StringUtils.isNotBlank(showType)){
				if(showType.equals("0")){
					exportChildhelp.setDisplay(null);
				}else{
					exportChildhelp.setDisplay(showType);
				}
			}
			exportChildhelp.setStartTime(TimeUtils.getFirstDayOfMonth(Integer.valueOf(yearstr),Integer.parseInt(monthstr)-1)+" "+"00:00:00");
			exportChildhelp.setEndTime(TimeUtils.getLastDayOfMonth(Integer.valueOf(yearstr),Integer.parseInt(monthstr)-1)+" "+"23:59:59");
			model.addAttribute("yearstr",  yearstr);
			model.addAttribute("monthstr", monthstr);
		//没有时间 则默认当月
		}else{
			Integer endMonth = Integer.parseInt(DateUtils.getMonth());
			exportChildhelp.setStartTime((Integer.valueOf(DateUtils.formatDate(new Date(), "yyyy")))+"-"+ (DateUtils.formatDate(new Date(), "MM"))+"-01"+" "+"00:00:00");
			exportChildhelp.setEndTime(TimeUtils.getLastDayOfMonth(Integer.valueOf(DateUtils.formatDate(new Date(), "yyyy")), Integer.valueOf(DateUtils.formatDate(new Date(), "MM"))-1)+" "+"23:59:59");
			model.addAttribute("yearstr",  DateUtils.formatDate(new Date(), "yyyy"));
			model.addAttribute("monthstr", endMonth-1);
		}
		model.addAttribute("showType",showType);
		model.addAttribute("liveProp",liveProp);
		model.addAttribute("username",UserUtils.getUser().getName());
		model.addAttribute("off", OfficeService.getFirstOffice().getName());
    	model.addAttribute("year",  DateUtils.formatDate(new Date(), "yyyy"));
		model.addAttribute("month", DateUtils.formatDate(new Date(), "MM"));
		model.addAttribute("date", DateUtils.formatDate(new Date(), "yyyy年MM月dd日"));
    }
    
}
